package com.example.myapplication.z_b_details

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.view.animation.TranslateAnimation
import android.widget.*
import androidx.appcompat.widget.Toolbar
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.mvp.view.BaseActivity
import com.example.myapplication.R
import com.example.myapplication.antivity.LoginActivity
import com.example.youhuo.entity.BaseEntity
import com.example.youhuo.mvp.contract.LjlContract
import com.example.youhuo.mvp.model.LjlModel
import com.example.youhuo.mvp.presenter.LjlPresenter
import com.example.youhuo.network.HttpType
import com.youth.banner.Banner
import com.youth.banner.loader.ImageLoader

class BuyActivity : BaseActivity<LjlPresenter>(), LjlContract.LjlView {
    private val buyTool: Toolbar by lazy { findViewById<Toolbar>(R.id.buy_tool) }
    private val buyDpImg: ImageView by lazy { findViewById<ImageView>(R.id.buy_dp_img) }
    private val buyDpTex: TextView by lazy { findViewById<TextView>(R.id.buy_dp_tex) }
    private val buyScImg: ImageView by lazy { findViewById<ImageView>(R.id.buy_sc_img) }
    private val buyScTex: TextView by lazy { findViewById<TextView>(R.id.buy_sc_tex) }
    private val buyBu: Button by lazy { findViewById<Button>(R.id.buy_bu) }

    private val buyBanner: Banner by lazy { findViewById<Banner>(R.id.buy_banner) }

    private var bannerlist: ArrayList<String>? = null
    private var path: String? = null
    private var parmes: ArrayList<Map<String, Any>>? = null


    override fun findlayout(): Int {
        return R.layout.activity_buy
    }

    override fun initView() {
        parmes = ArrayList()
        bannerlist = ArrayList()
        buyTool.setNavigationOnClickListener {
            finish()
        }
    }

    override fun initData() {
        mypre = LjlPresenter(LjlModel(),this)
        setBanner()
        setonclick()
        buybutton()

    }

    fun setBanner() {
        var stringExtra = intent.getStringExtra("imgpath")
        path = "http://iwenwiki.com/api/yoho/" + stringExtra
        bannerlist!!.add(path!!)
        bannerlist!!.add("http://iwenwiki.com/api/yoho//goods_img/goods_img1.jpg")

        buyBanner.setImages(bannerlist)
        buyBanner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {

                Glide.with(context).load(path).into(imageView)
            }
        })
        buyBanner.isAutoPlay(false)
        buyBanner.start()
    }

    fun setonclick() {
        buyScImg.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                buyScImg.setImageResource(R.mipmap.wujiaoxing2)
                buyScTex.setText("已收藏")
                buyScTex.setTextColor(Color.RED)
            }
        })
    }

    fun buybutton() {
        buyBu.setOnClickListener(View.OnClickListener {
            var boolean =
                getSharedPreferences("islog", Context.MODE_PRIVATE).getBoolean("log", false)
            if (boolean) {
                showcar()

            } else {
                startActivity(Intent(this, LoginActivity::class.java))
            }
        })
    }


    private fun showcar() {
        var dialog = Dialog(this)


        //获取布局
        var inflate = View.inflate(this, R.layout.botm_dialog, null)
        val botmImg: ImageView by lazy { inflate.findViewById<ImageView>(R.id.botm_img) }
        val botmColor1: TextView by lazy { inflate.findViewById<TextView>(R.id.botm_color1) }
        val botmS: TextView by lazy { inflate.findViewById<TextView>(R.id.botm_S) }
        val botmM: TextView by lazy { inflate.findViewById<TextView>(R.id.botm_M) }
        val botmL: TextView by lazy { inflate.findViewById<TextView>(R.id.botm_L) }
        val botmJian: TextView by lazy { inflate.findViewById<TextView>(R.id.botm_jian) }
        val botmShu: TextView by lazy { inflate.findViewById<TextView>(R.id.botm_shu) }
        val botmJia: TextView by lazy { inflate.findViewById<TextView>(R.id.botm_jia) }
        val botmBot: Button by lazy { inflate.findViewById<Button>(R.id.botm_bot) }
        botmBot.setOnClickListener(View.OnClickListener {
            addcar()
        })



        Glide.with(this).load(path).into(botmImg)

        //获取屏幕高度
        var disp = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(disp)
        var height = disp.heightPixels
        //创建动画
        var trans = TranslateAnimation(0F, 0F, height.toFloat(), 500F)
        trans.duration = 800
        //设置动画
        inflate.startAnimation(trans)
        //设置布局
        dialog.setContentView(inflate)
        //设置弹出位置
        var window = dialog.window
        var attributes = window!!.attributes
        attributes!!.gravity = Gravity.BOTTOM
        attributes!!.width = WindowManager.LayoutParams.MATCH_PARENT
        attributes!!.height = height / 2
        window.setBackgroundDrawable(ColorDrawable(0))
        window.attributes = attributes
        dialog.show()


    }

    fun addcar() {
        var map = HashMap<String, Any>()
        map.put("code", HttpType.ADDCAR)
        map.put("userid", "1")
        map.put("goodsid", "1")
        map.put("shopname", "1")
        map.put("shopcolor", "1")
        map.put("shopsize", "1")
        map.put("shopnum", "1")
        map.put("shopprice", "1")
        parmes!!.add(map)
        mypre.TabData(parmes!!)




    }


    override fun showmsg(entity: BaseEntity) {
        if (entity.msg.equals("添加成功")){
            ToastUtils.showShort("成功了")

        }else{
            ToastUtils.showShort("失败了")
        }

    }


}
